/**
 *  Java Api For Ecco Generated Applications 
 *  
 *  Copyright (C) 2000  by PDTec GmbH 
 *
 * @author  Michael Weirich
 */

package pdtec.ecco.meta;

/**
 * The class Type enables access to types defined in the Express schema.
 * Types may be entities, defined types or .... 
 * You can access the attribute information in the underlying ecco application
 * only by instantiation of Application or InstApplication.
 *
 * @see  pdtec.ecco.inst.InstApplication
 * @see  pdtec.ecco.meta.Application
 * @see  pdtec.ecco.meta.Entity
 */
public class Type {
    
    /**
     * This id actually represents a type object pointer.
     */
    protected long id;

    /**
     * Id of the the related application instance (either Application or InstApplication). Each application instance
     * has a set of Schemas. This relationship is expressed by this reference id.
     *
     * @see pdtec.ecco.meta.Application#getId
     */
    protected int appId;
    
    /**
     * The name of the schema.
     */
    protected String schemaName;

    /**
     * The name of the type.
     */
    protected String name;

    /**
     * The standard constructor is protected since instance creation is delegated
     * to the static {@link #get(int,long) get(...) } method. 
     * @see #get
     */
    protected  Type() {}

    /** Type constructor requires a valid application and a valid internal type.
     * @see #get
     */
    protected Type ( int appId,long typeId ) {
	this.appId = appId;
	this.id = typeId;
    } // constructor

    /**
     * Factory method for instance creation. Returns specified type.
     * The return value is either of class Entity or of class Type.
     * @param schemaName the name of the schema where the type is defined
     * @param typeId the type id
     * @return type or entity
     * @exception EccoException in case an error occurs
     */
    public static native synchronized Type get(int appId,long typeId) throws EccoException;

    /**
     * Factory method for instance creation. Returns specified type.
     * The return value is either of class Entity or of class Type.
     * @param schemaName the name of the schema where the type is defined
     * @param typeId the type id
     * @return type or entity
     * @exception EccoException in case an error occurs
     */
    public static  native synchronized Type getByName( int appId, String schemaName, String typeName ) throws EccoException;

    /** Returns full type name.
     * @exception EccoException in case an error occurs
     */
    public native synchronized String getFullName() throws EccoException;
    
    /** EccoString and EccoBinary have either a fixed length or are dynamic. 
     * @exception EccoException in case an error occurs
     */
    protected native synchronized boolean isFixed() throws EccoException;

    /** EccoString and EccoBinary usually have a length.
     * @exception EccoException in case an error occurs
     */
    protected native synchronized long getLength() throws EccoException;

    /** Returns a detailed list of types.
     * @exception EccoException in case an error occurs
     */
    public native synchronized Type[] exactTypeOf() throws EccoException;

    /** Removes all existing weak references in the internal heap of the ecco java api.
     * @exception EccoException in case an error occurs
     */
    protected native synchronized void delete() throws EccoException;
	    
    /**
     * Returns the type id.
     * @return the type id
     */
    public long getId() {
      return(id);
    } // getId
    
   /**
     * Returns the type name.
     * @return the type name
     */
    public String getName() {
	return(name); 
    } // getName

   /**
     * Returns the schema name.
     * @return the schema name
     */
    public String getSchemaName() {
	return(schemaName); 
    } // getSchemaName

    /** Returns the application id. */
    public int getAppId() {
    return(appId);
    } // getAppId

    /**
     * Finalizes this object and calls delete.
     * @see #delete
     */
    protected void finalize() {
	try { 
	    // throw (new EccoException("finalizing ..."));
	    if ( appId != -1) 
		delete(); 
	} catch(EccoException e) {
	    System.out.println("EccoException " + e.toString() );
	} // try
    } // finalize
    
} // class Type 
